package com.xucia.jsponic.security;

import java.security.Principal;
import java.security.acl.Acl;
import java.security.acl.AclEntry;
import java.security.acl.LastOwnerException;
import java.security.acl.NotOwnerException;
import java.security.acl.Permission;
import java.util.Enumeration;

import com.xucia.jsponic.data.Persistable;
import com.xucia.jsponic.data.PersistableImpl;
import com.xucia.jsponic.data.PersistentList;

public class PersistentAcl implements Acl {
	PersistableImpl aclPersistable;

	public PersistentAcl(Persistable aclPersistable) {
		this.aclPersistable = (PersistableImpl) aclPersistable;
	}

	public boolean addEntry(Principal caller, AclEntry entry) throws NotOwnerException {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean checkPermission(Principal principal, Permission permission) {
		if (permission instanceof PermissionLevel) {
	        PermissionLevel granted = PersistableImpl.computeAccessLevel(aclPersistable,principal);
        	return granted.level >= ((PermissionLevel)permission).level;
		}
		return false;
	}
	class SingleEnumeration implements Enumeration<Permission> {
		boolean more = true;
		PermissionLevel permission;
		SingleEnumeration(PermissionLevel permission) {
			this.permission = permission;
		}
		public boolean hasMoreElements() {
			return more;
		}

		public PermissionLevel nextElement() {
			more = false;
			return permission;
		}
		
	}
	public Enumeration<AclEntry> entries() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public Enumeration<Permission> getPermissions(Principal user) {
		return new SingleEnumeration(PersistableImpl.computeAccessLevel(aclPersistable, user));
	}

	public boolean removeEntry(Principal caller, AclEntry entry) throws NotOwnerException {
		// TODO Auto-generated method stub
		return false;
	}

	public void setName(Principal caller, String name) throws NotOwnerException {
		aclPersistable.set("name",name);
	}

	public boolean addOwner(Principal caller, Principal owner) throws NotOwnerException {
		throw new UnsupportedOperationException();
	}

	public boolean deleteOwner(Principal caller, Principal owner) throws NotOwnerException, LastOwnerException {
		throw new UnsupportedOperationException();
	}

	public boolean isOwner(Principal owner) {
		return false;
	}

	public interface TargetedComputation {
		void setUniversalPermissionLevel(int level);
		User getUser();
	}
    

	public String getName() {
		return (String) aclPersistable.get("name");
	}

	public Persistable getAclPersistable() {
		return aclPersistable;
	}
	
}
